﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.WmsApp.Base.DataBinding;
using MicropleSoft.WmsApp.Library.Factory;
using MicropleSoft.WmsApp.Base.Window;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.SecurityAuthentication
{
    public partial class FormUserManage : BaseForm
    {
        public FormUserManage()
        {
            InitializeComponent();
            if(GlobalConfig.RunTimeMode)
            {
                Binder.BindingGridViewLookUpEditYesOrNo(this.gridView1,
                                                        this.gridView1.Columns.ColumnByFieldName("IsDomainUser"),
                                                        "string");

                Binder.BindingGridViewLookUpEditYesOrNo(this.gridView1,
                                                     this.gridView1.Columns.ColumnByFieldName("IsLocked"),
                                                     "string");
                gridView1.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
                this.CmdQueryUser_Click(null, null);
            }
          
        }

        private void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var user = this.gridView1.GetRow(e.RowHandle) as WmsUser;
                if (user != null)
                {
                    if (user.DomainObjectState != DomainEntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }

        private void CmdAddUser_Click(object sender, EventArgs e)
        {
            var wmsUser = DomainObjectFactory.CreateWmsUser();
            if(this.xGridControl1.DataSource == null)
            {
                this.xGridControl1.DataSource = new List<WmsUser>();
            }
            ((List<WmsUser>)this.xGridControl1.DataSource).Add(wmsUser);
            this.xGridControl1.RefreshDataSource();

            for (int idx = 0; idx < ((List<WmsUser>)this.gridView1.DataSource).Count; idx++)
            {
                var u = (WmsUser)this.gridView1.GetRow(idx);

                if (u == wmsUser)
                {
                    this.gridView1.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdQueryUser_Click(object sender, EventArgs e)
        {
            try
            {
                var list = ServerProxy.Query<WmsUser>("QueryWmsUsers", " and 1=1");
                this.xGridControl1.DataSource = list;
            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdSaveUser_Click(object sender, EventArgs e)
        {
            try
            {
                //采用某些字段不更新的方法
                ServerProxy.Save<WmsUser>("SaveWmsUsers2", (List<WmsUser>)this.xGridControl1.DataSource, new List<string> { "Passwd" });
            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }

        }

        private void CmdDisableUser_Click(object sender, EventArgs e)
        {
            object obj = this.gridView1.GetRow(this.gridView1.FocusedRowHandle);
            if (obj != null)
            {
                string userId = ((WmsUser)obj).UserId;
                if (((WmsUser)obj).DomainObjectState == DomainEntityState.Added)
                {
                    XtraMessageBox.Show("该用户为新增用户，请先保存再禁用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (((WmsUser)obj).DomainObjectState == DomainEntityState.Updated)
                {
                    XtraMessageBox.Show("该用户已经修改，请先保存再禁用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                try
                {
                  
                }
                catch (Exception exception)
                {
                    Base.Exceptions.ExceptionHandler.ShowException(exception);
                }
            }
        }

        private void CmdSetPass_Click(object sender, EventArgs e)
        {
            object obj = this.gridView1.GetRow(this.gridView1.FocusedRowHandle);
            if (obj != null)
            {
                string userId = ((WmsUser) obj).UserId;
                if (((WmsUser) obj).DomainObjectState == DomainEntityState.Added)
                {
                    XtraMessageBox.Show("该用户为新增用户，请先保存再设置密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (((WmsUser)obj).DomainObjectState == DomainEntityState.Updated)
                {
                    XtraMessageBox.Show("该用户已经修改，请先保存再设置密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                try
                {
                    var formChangePass = new FormChangePass(true);
                    formChangePass.ShowDialog(this);
                }
                catch (Exception exception)
                {
                    Base.Exceptions.ExceptionHandler.ShowException(exception);
                }
            }
        }
    }
}